<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="crud.queryInput.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="crud.queryInput.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-date-picker
          v-model="query.planStartTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="计划起始时间范围"
          end-placeholder="计划起始时间范围"
        />
        <el-date-picker
          v-model="query.planFinishTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="计划结束时间范围"
          end-placeholder="计划结束时间范围"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="540px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item v-if="false" label="id">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="项目-阶段" prop="projectId">
            <project-stage-select :value="[form.projectId,form.stageId]" style="width: 370px;" @input="stageSelectInput" />
          </el-form-item>
          <el-form-item label="计划开始时间">
            <el-date-picker v-model="form.planStartTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="计划完成时间">
            <el-date-picker v-model="form.planFinishTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="描述信息" prop="sourceSection">
            <el-input v-model="form.sourceSection" type="textarea" :rows="3" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="分析文件路径">
            <el-input v-model="form.sourceFile" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="预计工作量">
            <el-input v-model="form.workload" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="难度权重">
            <el-input v-model="form.weight" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="任务在阶段占比">
            <el-input v-model="form.ratio" style="width: 370px;" />
          </el-form-item>
          <el-form-item v-if="form.id!=null" label="工作完成状态">
            <el-radio-group v-model="form.workState" style="width: 370px">
              <el-radio label="已完成">已完成</el-radio>
              <el-radio label="未完成">未完成</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        :row-class-name="rowClassSequenceNo"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('sequence')" prop="sequence" label="序号" width="80" />
        <el-table-column v-if="columns.visible('projectBase.projectName')" prop="projectBase.projectName" label="项目" />
        <el-table-column v-if="columns.visible('projectStage.stageName')" prop="projectStage.stageName" label="项目阶段" />
        <el-table-column v-if="columns.visible('sourceFile')" prop="sourceFile" label="分析文件" />
        <el-table-column v-if="columns.visible('sourceSection')" prop="sourceSection" label="来源章节和描述信息" width="400px" />
        <el-table-column v-if="columns.visible('planStartTime')" prop="planStartTime" label="计划开始时间" width="100px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('planFinishTime')" prop="planFinishTime" label="计划完成时间" width="100px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planFinishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('workload')" prop="workload" label="预计工作量" />
        <el-table-column v-if="columns.visible('weight')" prop="weight" label="难度权重" />
        <el-table-column v-if="columns.visible('ratio')" prop="ratio" label="任务在项目阶段占比" />
        <el-table-column v-if="columns.visible('workState')" prop="workState" label="工作完成状态" />
        <el-table-column v-permission="['admin','projectTask:edit','projectTask:del']" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProjectTask from '@/api/projectTask'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import projectStageSelect from '@/views/components/cascader/project_stage_cascader'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '项目任务', url: 'api/projectTask', sort: 'id,desc', crudMethod: { ...crudProjectTask }})
const defaultForm = { id: null, projectId: null, stageId: null, sourceFile: null, sourceSection: null, planStartTime: null, planFinishTime: null, workload: null, weight: null, ratio: null, workState: null }
export default {
  name: 'ProjectTask',
  components: { pagination, crudOperation, rrOperation, udOperation, projectStageSelect },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'projectTask:add'],
        edit: ['admin', 'projectTask:edit'],
        del: ['admin', 'projectTask:del']
      },
      rules: {
        projectId: [
          { required: true, message: '所属项目-阶段不能为空', trigger: 'blur' }
        ],
        sourceSection: [
          { required: true, message: '描述信息不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'projectId', display_name: '项目系统编号' },
        { key: 'projectName', display_name: '项目名称' },
        { key: 'stageName', display_name: '项目阶段' },
        { key: 'sourceSection', display_name: '来源章节和描述信息' },
        { key: 'workState', display_name: '工作完成状态' }
      ],
      pageCount: 0
    }
  },
  created() {
    this.crud.query.type = null
    this.crud.queryInput.value = null
    this.crud.query = {}
    // 设置默认搜索条件
    if (this.$route.query.mode === 'ONE') {
      this.crud.queryInput.type = 'projectId'
      this.crud.queryInput.value = this.$route.query.projectId
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      if (this.pageCount === 0) {
        if (this.$route.query.mode === 'ONE') {
          this.crud.queryInput.type = 'projectId'
          this.crud.queryInput.value = this.$route.query.projectId
        }
        this.pageCount = this.pageCount + 1
      }
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      return true
    },
    rowClassSequenceNo({ row, rowIndex }) {
      // 把每一行的索引放进row.id
      row.sequence = rowIndex + 1
    },
    stageSelectInput(e) {
      this.form.projectId = e[0]
      this.form.stageId = e[1]
    }
  }
}
</script>

<style scoped>

</style>
